package com.springboot.cloud.demos.feign.rest;

import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.demos.entity.form.MessageForm;
import com.springboot.cloud.demos.entity.form.MessageQueryForm;
import com.springboot.cloud.demos.entity.form.UserQueryForm;
import com.springboot.cloud.demos.entity.po.Message;
import com.springboot.cloud.demos.entity.po.User;
import com.springboot.cloud.demos.feign.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class MessageController extends BaseController {

    @Autowired
    private MessageService messageService;

    @RequestMapping(value = "/message/{id}", method = RequestMethod.GET)
    public Result get(@PathVariable(value = "id") String id) {
        return messageService.get(id);
    }

    @RequestMapping(value = "/message/{id}", method = RequestMethod.PUT)
    public Result update(@PathVariable(value = "id") String id) {
        Result<Message> messageResult = messageService.get(id);
        if (messageResult.isSuccess()) {
            Message message = new Message();
            message.setIsRead(true);
            message.setId(id);
            return messageService.update(message);
        }
        return  Result.fail();

    }

    @GetMapping("/message/list")
    public Result list(HttpServletRequest request, Integer type, Integer pageSize, Integer pageNo) {
        User user = getUser(request);
        MessageQueryForm queryForm = new MessageQueryForm();
        queryForm.setCurrent(pageNo);
        queryForm.setSize(pageSize);
        queryForm.setType(type);
        queryForm.setToUserId(user.getId());
        return messageService.list(queryForm);
    }

    @GetMapping("/message/count")
    public Result count(HttpServletRequest request, Integer type) {
        User user = getUser(request);
        MessageQueryForm queryForm = new MessageQueryForm();
        queryForm.setType(type);
        queryForm.setToUserId(user.getId());
        return messageService.count(queryForm);
    }
}
